﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SongsDB;
using System.Speech.Recognition;

namespace MMVoiceComm
{
    public partial class SelectionForm : Form
    {
        public delegate void SelectionCompleteHandler(SDBSongData[] SelectedSongList);
        public event SelectionCompleteHandler OnSelectionComplete;
        private SDBSongData[] Songs;
        private SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine();
        public SelectionForm(SDBSongData[] SelectedSongList)
        {
            InitializeComponent();
            recognitionEngine.SetInputToDefaultAudioDevice();
            Songs = SelectedSongList;
            
        }

        private void SelectionForm_Load(object sender, EventArgs e)
        {
            try
            {
                Choices numberChoices = new Choices();

                int c = 0;
                foreach (SDBSongData song in Songs)
                {
                    SongSelectionBox.Items.Add(c + ": " + song.ArtistName + " - " + song.Title);
                    SongSelectionBox.SetItemChecked(SongSelectionBox.Items.Count - 1, true);
                    numberChoices.Add(c.ToString());
                    c++;

                }
                recognitionEngine.LoadGrammar(new Grammar(new GrammarBuilder(numberChoices)));
                Logger.log("numbers loaded");
                Choices commands = new Choices();
                commands.Add("done");
                commands.Add("next page");
                commands.Add("previous page");
                commands.Add("shrink");
                commands.Add("all");
                commands.Add("zilch");
                commands.Add("enlarge");
                Logger.log("commands generated");
                recognitionEngine.LoadGrammar(new Grammar(new GrammarBuilder(commands)));
                Logger.log("commands loaded into engine");
                recognitionEngine.SpeechRecognized +=
                    new EventHandler<SpeechRecognizedEventArgs>(recognitionEngine_SpeechRecognized);
                recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
            }catch(Exception ex)
            {
                Logger.log(ex.Message);
            }
        }

        void recognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if(e.Result.Confidence > .8)
            {//Confident!!!
                if(e.Result.Text.Equals("done"))
                {
                    Logger.log("Done!");
                    this.Close();
                }
                else if (e.Result.Text.Equals("all"))
                {
                    for(int i=0; i<SongSelectionBox.Items.Count;i ++)
                    {
                        SongSelectionBox.SetItemChecked(i,true);
                    }
                }
                else if (e.Result.Text.Equals("zilch") )
                {
                    for (int i = 0; i < SongSelectionBox.Items.Count; i++)
                    {
                        SongSelectionBox.SetItemChecked(i, false);
                    }
                }
                else if (e.Result.Text.Equals("shrink"))
                {
                    try
                    {
                        System.Drawing.Font f = new Font(SongSelectionBox.Font.FontFamily,
                                                         SongSelectionBox.Font.Size - 5);
                        SongSelectionBox.Font = f;
                    }
                    catch (Exception ex)
                    {
                        Logger.log(ex.Message);
                    }
                }
                else if (e.Result.Text.Equals("enlarge"))
                {
                    try
                    {
                        System.Drawing.Font f = new Font(SongSelectionBox.Font.FontFamily,
                                                         SongSelectionBox.Font.Size + 5);
                        SongSelectionBox.Font = f;
                    }
                    catch (Exception ex)
                    {
                        Logger.log(ex.Message);
                    }
                }
                else if (e.Result.Text.Equals("previous page"))
                {
                    try
                    {
                        if ((SongSelectionBox.TopIndex - ((int)(SongSelectionBox.Height / SongSelectionBox.ItemHeight))) > 0)
                        {
                            SongSelectionBox.TopIndex -= ((int) (SongSelectionBox.Height/SongSelectionBox.ItemHeight));
                        }else
                        {
                            SongSelectionBox.TopIndex = 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.log(ex.Message);
                    }
                }
                else if (e.Result.Text.Equals("next page"))
                {
                    try
                    {
                        SongSelectionBox.TopIndex += ((int)(SongSelectionBox.Height / SongSelectionBox.ItemHeight));
                    }
                    catch (Exception ex)
                    {
                        Logger.log(ex.Message);
                    }
                }
                else
                {
                    try
                    {
                        int itemIndex = Convert.ToInt32(e.Result.Text);
                        //reverse check
                        SongSelectionBox.SetItemChecked(itemIndex, SongSelectionBox.GetItemChecked(itemIndex) ? false : true);
                    }
                    catch (Exception ex)
                    {
                        Logger.log(ex.Message);
                    }
                }
            }
        }

        private void SelectionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            List<SDBSongData> TmpSongList = new List<SDBSongData>();
            for(int i =0; i<SongSelectionBox.CheckedIndices.Count;i++)
            {
                Logger.log(Songs[SongSelectionBox.CheckedIndices[i]].Title);
                TmpSongList.Add(Songs[SongSelectionBox.CheckedIndices[i]]);
            }
            SelectionComplete(TmpSongList.ToArray());
        }
        protected void SelectionComplete(SDBSongData[] SongList)
        {
            if (OnSelectionComplete != null)
            {
                OnSelectionComplete(SongList);
            }
        }
    }
}
